package com.timk.goserver.client.ui.widgets;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.timk.goserver.client.ui.screens.UserListDialog;

/**
 * TextBox with browse for user
 * @author TKington
 *
 */
public class ChooseUserText extends Composite implements UserLinkListener {
	private final TextBox userText;
	
	/**
	 * Creates a ChooseUserText
	 */
	public ChooseUserText() {
		HorizontalPanel panel = new HorizontalPanel();
		
		userText = new TextBox();
		panel.add(userText);
		userText.setWidth("100%");
		panel.setCellWidth(userText, "100%");
		
		Button browseButton = new Button("...");
		browseButton.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				onBrowse();
			}
		});
		panel.add(browseButton);
		
		initWidget(panel);
	}
	
	void onBrowse() {
		UserListDialog dlg = new UserListDialog(this);
		dlg.show();
	}

	public void linkClicked(String username) {
		if(userText.isEnabled())
		{
			userText.setText(username);
		}
	}
	
	/**
	 * Sets enabled state
	 * @param enabled
	 */
	public void setEnabled(boolean enabled) {
		userText.setEnabled(enabled);
	}
	
	/**
	 * Returns the chosen user
	 * @return the chosen user
	 */
	public String getUser() {
		return userText.getText();
	}
	
	/**
	 * Sets the chosen user
	 * @param text
	 */
	public void setUser(String text) {
		userText.setText(text);
	}

	/**
	 * Returns the underlying TextBox
	 * @return the underlying TextBox
	 */
	public TextBox getTextBox() {
		return userText;
	}
}
